package ThreadPool.lagou.concurrent.demo01;

import java.util.concurrent.*;

/**
 * @author oyz
 * @version 1.0.4
 * @description: MyCallable实现线程
 * @date 2022/5/23 21:18
 */
public class MyCallable<T> implements Callable<T> {

    @Override
    public T call() throws Exception {
        Thread.sleep(2000);
        return (T) "call方法的返回值";
    }


     ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5,5, 1,
            TimeUnit.SECONDS,new ArrayBlockingQueue<>(10)){
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            super.afterExecute(r, t);
            System.out.println("处理线程执行之后的结果"+t);
        }

    };

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable<Object> objectMyCallable = new MyCallable<>();
        FutureTask futureTask = new FutureTask(objectMyCallable);
        new Thread(futureTask).start();
        // mian线程会同步等待,理解为join会等待其他线程执行完毕
        System.out.println(futureTask.get());
        System.out.println("main线程运行结束!");
        System.out.println("--------------");
        Future<Object> future = new MyCallable<>().poolExecutor.submit(new MyCallable<>());
        Object o = future.get();
        System.out.println(o);
        System.out.println("主线程执行完毕");
        System.exit(0);
    }


}
